package com.example.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.reggie.entity.*;
import com.example.reggie.mapper.OrdersMapper;
import com.example.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl extends ServiceImpl<OrdersMapper, Orders>implements OrderService {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Override
    @Transactional
    public Boolean sumbit(Orders order) {
        LambdaQueryWrapper<ShoppingCart> shoppingQuery = new LambdaQueryWrapper<>();
          shoppingQuery.eq(ShoppingCart::getUserId, order.getUserId());
        User user = userService.getById(order.getUserId());
        AddressBook addressBook = addressBookService.getById(order.getAddressBookId());
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(shoppingQuery);
     AtomicInteger amount=new AtomicInteger();
        long orderNumber = IdWorker.getId();

        List<OrderDetail> orderDetailList  =   shoppingCartList.stream().map(item->{
               OrderDetail orderDetail = new OrderDetail();
               orderDetail.setId(item.getId());
               orderDetail.setDishId(item.getDishId());
               orderDetail.setOrderId(orderNumber);
               orderDetail.setSetmealId(item.getSetmealId());
               orderDetail.setNumber(item.getNumber());
               orderDetail.setAmount(item.getAmount());
               orderDetail.setName(item.getName());
               orderDetail.setImage(item.getImage());
               orderDetail.setDishFlavor(item.getDishFlavor());
               amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
              return orderDetail;
           }).collect(Collectors.toList());
        order.setId(orderNumber);
        order.setNumber(String.valueOf(orderNumber));
        order.setOrderTime(LocalDateTime.now());
        order.setCheckoutTime(LocalDateTime.now());
        order.setStatus(2);
        order.setPhone(user.getPhone());
        order.setUserName(user.getName());
        order.setAddress(addressBook.getDetail());
        order.setAmount(new BigDecimal(amount.get()));//总金额
        order.setPayMethod(order.getPayMethod());
        order.setConsignee(addressBook.getConsignee());
        this.save(order);

        orderDetailService.saveBatch(orderDetailList);

       return shoppingCartService.remove(shoppingQuery);

    }


}
